package com.feicheng.controller.wx;


import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Adz;
import com.feicheng.persistence.entity.Client;
import com.feicheng.persistence.service.IClientService;
import com.meituan.sdk.DefaultMeituanClient;
import com.meituan.sdk.MeituanClient;
import com.meituan.sdk.MeituanResponse;
import com.meituan.sdk.auth.MeituanTokenResponse;
import com.meituan.sdk.internal.exceptions.MtSdkException;
import com.meituan.sdk.model.ddzh.tuangou.tuangouDealQueryshopdeal.RshopDeal;
import com.meituan.sdk.model.ddzh.tuangou.tuangouDealQueryshopdeal.TuangouDealQueryshopdealRequest;
import com.meituan.sdk.model.ddzh.tuangou.tuangouDealQueryshopdeal.TuangouDealQueryshopdealResponse;
import com.meituan.sdk.model.ddzh.tuangou.tuangouReceiptQuerybymobile.TuangouReceiptQuerybymobileRequest;
import com.meituan.sdk.model.tuangouNg.coupon.couponQueryLocalListByDate.CouponQueryLocalListByDateRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2024-08-21
 */
@RestController("/wx/client")
@RequestMapping("/wx/client")
public class ClientController {

    @Autowired
    IClientService iClientService;


    @PostMapping("/info/put")
    public ResultInfo updateClient(@RequestBody JSONObject param) {
        Client client = param.getObject("client", Client.class);
        if(!StringUtils.hasLength(client.getClientid())){
            throw new SystemException("店长id不能为空");
        }
        iClientService.updateById(client);
        param.put("client", client);
        return ResultInfo.success(param);
    }

    @PostMapping("/mt/hxq/get")
    public ResultInfo getMthxq(@RequestBody JSONObject param) {

        Client client = param.getObject("client", Client.class);


        Long developerId = 117165L;
        String signKey = "c1bhx9smco8xps0b";
        String appAuthToken = "V2-64b9a3a8d9852063655721afa1286d042415763d9d95f7fd5311ed7a22f3e8928d025fe60e4e9014af13994460f59790ac1dde0d38bf07e375dd6637b12ce1ebf3522d74de68e16c59cd6ec7aa7a514fd5eb9da2fa792eb949739ba453096a44";
        //构造meituanClient，推荐使用单例方式，一个develoepr只使用一个实例
        MeituanClient meituanClient = DefaultMeituanClient.builder(developerId, signKey).build();

        TuangouDealQueryshopdealRequest request = new TuangouDealQueryshopdealRequest();
        request.setSource(2);
        request.setOffset(1);
        request.setLimit(10);

        try {
            //发起接口调用
            MeituanResponse<TuangouDealQueryshopdealResponse> response = meituanClient.invokeApi(request, appAuthToken);
            //判断是调用是否成功
            if (response.isSuccess()) {
                //调用成功，通过getData获取接口响应数据
                TuangouDealQueryshopdealResponse tuangouDealQueryshopdealResponse = response.getData();
                List<RshopDeal> shopDeals = tuangouDealQueryshopdealResponse.getResult();
                for(RshopDeal shopDeal: shopDeals) {
                    System.out.println(shopDeal);
                }
            } else {
                //调用失败，通过getCode和getMsg获取错误码和错误描述
                System.out.println(response.getCode());
                System.out.println(response.getMsg());
            }
        } catch (MtSdkException e) {
            e.printStackTrace();
        }

        return ResultInfo.success(param);
    }





}

